package com.clubs.coding.Controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.tree.Tree;
import com.clubs.coding.Service.AthAdminUserService;
import com.clubs.coding.Service.NewsService;
import com.clubs.coding.Service.UserService;
import com.clubs.coding.common.ApiRestResponse;
import com.clubs.coding.common.Constant;
import com.clubs.coding.exception.ImoocMallException;
import com.clubs.coding.exception.ImoocMallExceptionEnum;
import com.clubs.coding.model.Vo.MenusVo;
import com.clubs.coding.model.pojo.AthAdminUser;
import com.clubs.coding.model.pojo.News;
import com.clubs.coding.model.pojo.User;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@ResponseBody


public class AthAdminUserController {
    private String code1;
    @Autowired
    AthAdminUserService athAdminUserService;


    @GetMapping("/test")

    public ApiRestResponse test(){
        return ApiRestResponse.success();
    }
    /*登录接口*/
    @PostMapping("/admin/login")//登录接口
    public ApiRestResponse login(@RequestParam("userName") String userName, @RequestParam("password") String password, String code,HttpSession session,ServletRequest servletRequest) throws ImoocMallException {

        //获得存储在session中的验证码
        String sessionCheckCode = (String) session.getAttribute("code");
        HttpServletRequest request = (HttpServletRequest) servletRequest;//转化为HttpServletRequest
        HttpSession session1 = request.getSession();//拿到session
//       System.out.println(session1 +"xxxxxxx");
//        System.out.println(sessionCheckCode +"sdasfasd");
        try{
            //判断验证码是否正确
//            if (code==null || !sessionCheckCode.equals(code)) {
//                //登陆失败，提示验证码不正确！
//                return ApiRestResponse.error(300, "验证码错误");
//            }
            //本地情况先用这个测试
            if (code==null || !code1.equals(code)) {
                //登陆失败，提示验证码不正确！
                return ApiRestResponse.error(300, "验证码错误");
            }
        }catch (Exception e){
            return ApiRestResponse.error(300,"不存在session,请在同一域名调试");
        }



        if(StringUtils.isNullOrEmpty(userName)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isNullOrEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        AthAdminUser user = athAdminUserService.login(userName, password);
        //保存用户信息不报错密码
        user.setPassWord(null);
        session.setMaxInactiveInterval(86400*30);//设置session一个月后失效
        session.setAttribute(Constant.ATH_CLUBS_USER,user);//kye value
        return ApiRestResponse.success(user);
    }
    @PostMapping("/admin/alter")//修改密码
    public ApiRestResponse alter(@RequestParam("userName") String userName, @RequestParam("passwordold") String passwordold,@RequestParam("password") String password){
        athAdminUserService.alter(userName,passwordold,password);
        return ApiRestResponse.success("修改成功");
    }
    /**
     * 得到登陆验证码
     * @param response
     * @param session
     * @throws
     */
    @RequestMapping("/admin/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);
        System.out.println("验证码为"+lineCaptcha.getCode());
        //将验证码放入session
        code1 = lineCaptcha.getCode();
        session.setAttribute("code",lineCaptcha.getCode());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
//    public ApiRestResponse getCode(HttpServletResponse response, HttpSession session) throws IOException {
//        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);
//        System.out.println("验证码为"+lineCaptcha.getCode());
//        //将验证码放入session
//        code1 = lineCaptcha.getCode();
//      session.setAttribute("code",lineCaptcha.getCode());
//        try {
//            ServletOutputStream outputStream = response.getOutputStream();
//            lineCaptcha.write(outputStream);
//            outputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ApiRestResponse.success();
//    }
    /**
     * 会员与游客列表
     * **/
    @PostMapping("/admin/member")
    public ApiRestResponse member(
                                  Integer pageNum,
                                  Integer pageSize,
                                  @RequestParam(value = "id",required = false ) Integer id,
                                  @RequestParam(value = "nickName",required = false ) String nickName,
                                  @RequestParam(value = "member",defaultValue = "1") Integer member,
                                  @RequestParam(value = "mobile",required = false ) Integer mobile

                                  ){

        PageInfo pageInfo = athAdminUserService.member(pageNum, pageSize, member, id, nickName, mobile);
        return ApiRestResponse.success(pageInfo);
    }
    /**
     * 菜单
     * **/
    @PostMapping("/admin/mian/menus")
    public ApiRestResponse menus(){
        List<Tree<String>> treeList = athAdminUserService.menus();
        return ApiRestResponse.success(treeList);
    }
    /**
     * 用户表的修改
     * **/
    @PostMapping("/admin/user")
    public ApiRestResponse userEd( @RequestBody User user){
        athAdminUserService.userEd(user);
        return ApiRestResponse.success();
    }
}
